package com.zqxq.lottery.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zqxq.common.bo.BaseBO;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.config.YmlConfig;
import com.zqxq.lottery.entity.bo.sys.SysDiscoveryCMSBO;
import com.zqxq.lottery.entity.o2o.sys.SysDiscoveryO2O;
import com.zqxq.lottery.entity.o2o.sys.SysMsgO2O;
import com.zqxq.lottery.entity.po.sys.SysDiscoveryPO;
import com.zqxq.lottery.entity.po.sys.SysMsgPO;
import com.zqxq.lottery.entity.vo.sys.SysDiscoveryVO;
import com.zqxq.lottery.entity.vo.sys.SysMsgVO;
import com.zqxq.lottery.service.sys.ISysDiscoveryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/sysDiscovery")
public class SysDiscoveryController extends SysBaseController{

    @Autowired
    private ISysDiscoveryService sysDiscoveryService;
    @Autowired
    private YmlConfig ymlConfig;

    @ApiOperation("获取分页列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysDiscoveryVO>> queryPageList(@RequestBody SysDiscoveryCMSBO cmsbo){
        Result result = new Result();
        try{
            Page<SysDiscoveryPO> page = new Page(cmsbo.getPageNo(), cmsbo.getPageSize());
            Wrapper wrapper = new EntityWrapper();
            if(StrUtils.isNotEmpty(cmsbo.getSearchKey())){
                wrapper.like("theme", cmsbo.getSearchKey());
            }
            wrapper.orderBy("sort", false);
            wrapper.eq("status",1);
            page = sysDiscoveryService.selectPage(page, wrapper);
            Page<SysDiscoveryVO> voPage = SysDiscoveryO2O.toPageVO(page);
            result.setData(voPage);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public Result save(@RequestBody SysDiscoveryVO vo){
        Result result = new Result();
        try{
            SysDiscoveryPO po = SysDiscoveryO2O.toPO(vo);
            if(vo.getId() == null){
                Long id = ymlConfig.getPkId();
                po.setId(id);
            }
            sysDiscoveryService.insertOrUpdate(po);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
            result.setCode(CodeEnum.FAIL_100001.getCode());
            result.setMsg(CodeEnum.FAIL_100001.getMsg());
        }
        return result;
    }

    @ApiOperation("获取列表")
    @PostMapping("/queryList")
    public Result<List<SysDiscoveryVO>> queryList(){
        Result result = new Result();
        try{
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("status", 1);
            wrapper.orderBy("sort", false);
            List<SysDiscoveryPO> poList = sysDiscoveryService.selectList(wrapper);
            List<SysDiscoveryVO> voList = SysDiscoveryO2O.toVOList(poList);
            result.setData(voList);
        } catch (Exception e){
            log.error("SysBannerController queryPageList", e);
        }
        return result;
    }

    @ApiOperation("关闭发现")
    @PostMapping(value = "/close")
    public Result closeDiscovery(Long id){
        if (id == null){
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        }
        try {
            SysDiscoveryPO sysDiscoveryPO = new SysDiscoveryPO();
            sysDiscoveryPO.setId(id);
            sysDiscoveryPO.setStatus(0);
            sysDiscoveryService.updateById(sysDiscoveryPO);
        } catch (Exception e){
            log.error("SysDiscoveryController close",e);
            return new Result(CodeEnum.FAIL_100001.getCode(),CodeEnum.FAIL_100001.getMsg());
        }
        return new Result();
    }

}
